//mongodb
// const mongoose=require('mongoose')
import mongoose from 'mongoose';
const express=require('express');
const bodyParser = require('body-parser');
const cors = require('cors');
// import express from 'express';
mongoose.connect('mongodb://localhost:27017/student')


const List=mongoose.model('List',new mongoose.Schema({
    name:{type:String},
    age:{type:Number},
    sex:{type:String},
    hobby:[{type:String}],
    school:{type:String}
}))

class Student{
    name: string;
    age: number;
    sex: string;
    hobby: string[];
    school: string;

    constructor(name: string,age: number,sex: string,hobby: string[],school: string){
        this.name=name;
        this.age=age;
        this.sex=sex;
        this.hobby=hobby;
        this.school=school;
    }

}

async function addmongodb(na:string,ag:number,se:string,ho:string[],sc:string)
{
    // console.log("haha");
    await List.insertMany(
        {name:na,age:ag,sex:se,hobby:ho,school:sc}
    );
    // const list =await List.find();
    // console.log(list);
}

async function updatemongodb(na:string,ag:number,se:string,ho:string[],sc:string)
{
    // console.log("haha");
    await List.updateOne(
        {name:na},{$set:{age:ag,sex:se,hobby:ho,school:sc}}
    );
    // const list =await List.find();
    // console.log(list);
}

async function delmongodb(na:string,ag:number)
{
    // console.log("haha");
    await List.deleteOne(
        {name:na},{age:ag}
    );
    // const list =await List.find();
    // console.log(list);
}

// 创建 Express 应用
const app = express();
// 添加这一行来允许所有跨源请求
app.use(cors()); 
// 设置框架中间件解析 JSON 格式数据
app.use(bodyParser.json());

// 简单的 post 接口
app.post('/api/adddata', (req) => {
    // console.log(req.body.data.name);
    const st=new Student(req.body.data.name,req.body.data.age,req.body.data.sex,req.body.data.hobby,req.body.data.school);
    addmongodb(st.name,st.age,st.sex,st.hobby,st.school);
});
app.post('/api/updatedata', (req) => {
    // console.log(req.body.data.name);
    const st=new Student(req.body.data.name,req.body.data.age,req.body.data.sex,req.body.data.hobby,req.body.data.school);
    updatemongodb(st.name,st.age,st.sex,st.hobby,st.school);
});
app.post('/api/deldata', (req) => {
    // console.log(req.body.data.name);
    const st=new Student(req.body.data.name,req.body.data.age,req.body.data.sex,req.body.data.hobby,req.body.data.school);
    delmongodb(st.name,st.age);
});
app.post('/api/getdata1', (req,res) => {
    // console.log(req.body.param1);
    // console.log(req.body.param2);
    // console.log(req.body.param3);
    if(req.body.type=="姓名")
    {
        List.find({name:req.body.param1}).sort({name:req.body.ns,age:req.body.as}).skip((req.body.param2-1)*req.body.param3).limit(req.body.param3).then((lists)=>{
            res.json({ message: lists });
            // console.log(lists);
        })
    }
    else if(req.body.type=="年龄")
    {
        List.find({age:req.body.param1}).sort({name:req.body.ns,age:req.body.as}).skip((req.body.param2-1)*req.body.param3).limit(req.body.param3).then((lists)=>{
            res.json({ message: lists });
            // console.log(lists);
        })
    }
    else if(req.body.type=="性别")
    {
        List.find({sex:req.body.param1}).sort({name:req.body.ns,age:req.body.as}).skip((req.body.param2-1)*req.body.param3).limit(req.body.param3).then((lists)=>{
            res.json({ message: lists });
            // console.log(lists);
        })
    }
    else
    {
        List.find({school:req.body.param1}).sort({name:req.body.ns,age:req.body.as}).skip((req.body.param2-1)*req.body.param3).limit(req.body.param3).then((lists)=>{
            res.json({ message: lists });
            // console.log(lists);
        })
    }
});
app.post('/api/gettotal1', (req,res) => {
    getListCount1(req.body.data,req.body.type).then((count) => {
        // console.log('Result count:', count);
        res.json({ message: count });
    });
});
app.post('/api/getdata', (req,res) => {
    // res.json({ message: '成功获取数据' });
    // console.log(req.body.data1);
    List.find().sort({name:req.body.ns,age:req.body.as}).skip((req.body.data-1)*req.body.data1).limit(req.body.data1).then((lists)=>{
        res.json({ message: lists });
    })
});


// 简单的 GET 接口
app.get('/api/gettotal', (req,res) => {
    // res.json({ message: '成功获取数据' });
    getListCount().then((count) => {
        // console.log('Result count:', count);
        res.json({ message: count });
    });
});

async function getListCount(){
    const count = await List.countDocuments();
    // console.log(count)
    return count;
}
async function getListCount1(c,t){
    if(t=="姓名")
    {
        const query = { name: c }; 
        const count = await List.countDocuments(query);
        return count;
    }
    else if(t=="年龄")
    {
        const query = { age: c }; 
        const count = await List.countDocuments(query);
        return count;
    }
    else if(t=="性别")
    {
        const query = { sex: c }; 
        const count = await List.countDocuments(query);
        return count;
    }
    else
    {
        const query = { school: c }; 
        const count = await List.countDocuments(query);
        return count;
    }
    // console.log(count);
}


// 启动服务器
const port = 3000;
app.listen(port, () => {
  console.log(`服务器运行在 http://localhost:${port}`);
});